package com.py.credit.controller;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.py.credit.common.ServiceException;
import com.py.credit.common.logs.ExceptionLogger;
import com.py.credit.common.rest.RestServiceResult;
import com.py.credit.entity.CreditReportLog;
import com.py.credit.entity.SysUser;
import com.py.credit.entity.WarnRuleEntity;
import com.py.credit.entity.WarnSchemeEntity;
import com.py.credit.service.CreditReportService;
import com.py.credit.service.GetRoleService;
import com.py.credit.service.OutInterfaceService;
import com.py.credit.service.RemoteBaseService;
import com.py.credit.utils.FileUtil;
import com.py.credit.utils.UuidUtil;
import com.py.framework.core.annotation.IgnoreAuthority;
import com.py.framework.core.common.Page;
import com.py.framework.core.helper.HttpServletHelper;

@RestController
@RequestMapping(value = "/outinterface")
public class OutInterfaceController extends BaseController {
	@Autowired
	private RemoteBaseService remoteBaseService;
	@Autowired
	private OutInterfaceService outInterfaceService;
	@Autowired
	private CreditReportService creditReportService;
	@Value("${absolutepath}")
	private String absolutepath;
	@Autowired
    private GetRoleService getRoleService;
	/**
	 * 列表展示企业信息
	 * @param name
	 * @param page
	 * @param kId
	 * @return
	 */
	@RequestMapping(value = "/getCompanyInfo.json", method = RequestMethod.GET)
	public RestServiceResult<Object> getCompanyInfo(String name, Page page, String kId) {

		RestServiceResult<Object> r = new RestServiceResult<>();
		try {
			r.setData(outInterfaceService.getCompanyInfo(name, kId, page));
			r.setCount(page.getTotalCount());
		} catch (Exception e) {
			r.setCode(RestServiceResult.CODE_UNKNOW);
			r.setMsg("系统异常！");
		}
		return r;
	}

	/**
	    *        添加规则
	 * @param entity
	 * @return
	 */
	@RequestMapping(value = "/addRule.json", method = RequestMethod.POST)
	public RestServiceResult<Object> addRule(@RequestBody WarnRuleEntity entity,HttpServletRequest request) {
		RestServiceResult<Object> r = new RestServiceResult<>();
		String token = HttpServletHelper.getUserToken(request);
		RestServiceResult<SysUser> user = remoteBaseService.getCurrentUser(token);
		SysUser userInfo = null;
		if (user != null) {
			userInfo = user.getData();
		} else {
			r.setMsg("系统异常！");
			return r;
		}
		entity.setId(UuidUtil.getInstance().getUUid());
		entity.setUserId(String.valueOf(userInfo.getId()));
		entity.setDeptId(String.valueOf(userInfo.getOfficeId()));
		try {
			outInterfaceService.addRule(entity);
			r.setMsg("添加规则成功！");
		} catch (Exception e) {
			r.setCode(RestServiceResult.CODE_UNKNOW);
			r.setMsg("系统异常！");
		}
		return r;
	}

	/**
	   *      添加方案
	 * @param entity
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/addScheme.json", method = RequestMethod.POST)
	public RestServiceResult<Object> addScheme(
			@RequestParam(value = "name", defaultValue = "",required = true)String name,
			@RequestParam(value = "type", defaultValue = "",required = true)String type,
			@RequestParam(value = "mainType", defaultValue = "",required = true)String mainType,
			@RequestParam(value = "description", defaultValue = "",required = true)String description,
			@RequestParam(value = "tybms", defaultValue = "",required = false)List<String> tybms,
            HttpServletRequest request) {
		RestServiceResult<Object> r = new RestServiceResult<>();
		String token = HttpServletHelper.getUserToken(request);
		RestServiceResult<SysUser> user = remoteBaseService.getCurrentUser(token);
		SysUser userInfo = null;
		
		if (user != null) {
			userInfo = user.getData();
		} else {
			r.setMsg("系统异常！");
			return r;
		}
		try {
			WarnSchemeEntity entity = new WarnSchemeEntity();
			entity.setName(name);
			entity.setDescription(description);
			entity.setMainType(mainType);
			entity.setType(type);
			entity.setDeptId(String.valueOf(userInfo.getOfficeId()));
			entity.setUserId(String.valueOf(userInfo.getId()));
			outInterfaceService.addScheme(entity,tybms,null);
			r.setMsg("添加方案成功！");
		} catch (Exception e) {
			r.setCode(RestServiceResult.CODE_UNKNOW);
			r.setMsg("系统异常！");
		}
		return r;
	}

	/**
	   *      添加方案
	 * @param entity
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/addSchemeAll.json", method = RequestMethod.POST)
	public RestServiceResult<Object> addSchemeAll(String name,String type,String mainType,String description,
          HttpServletRequest request) {
		RestServiceResult<Object> r = new RestServiceResult<>();
		String token = HttpServletHelper.getUserToken(request);
		RestServiceResult<SysUser> user = remoteBaseService.getCurrentUser(token);
		SysUser userInfo = null;
		
		if (user != null) {
			userInfo = user.getData();
		} else {
			r.setMsg("系统异常！");
			return r;
		}
		try {
			WarnSchemeEntity entity = new WarnSchemeEntity();
			entity.setName(name);
			entity.setDescription(description);
			entity.setMainType(mainType);
			entity.setType(type);
			entity.setDeptId(String.valueOf(userInfo.getOfficeId()));
			entity.setUserId(String.valueOf(userInfo.getId()));
			outInterfaceService.addScheme(entity,null,null);
			r.setMsg("添加方案成功！");
		} catch (Exception e) {
			r.setCode(RestServiceResult.CODE_UNKNOW);
			r.setMsg("系统异常！");
		}
		return r;
	}

	
	/**
	   *      添加方案
	 * @param entity
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/addSchemeByFile.json", method = RequestMethod.POST)
	public RestServiceResult<Object> addSchemeByFile(
			@RequestParam(value = "name", defaultValue = "",required = true)String name,
			@RequestParam(value = "type", defaultValue = "",required = true)String type,
			@RequestParam(value = "mainType", defaultValue = "",required = true)String mainType,
			@RequestParam(value = "description", defaultValue = "",required = true)String description,
			@RequestParam(value = "file", defaultValue = "",required = false)MultipartFile file,
          HttpServletRequest request) {
		RestServiceResult<Object> r = new RestServiceResult<>();
		String token = HttpServletHelper.getUserToken(request);
		RestServiceResult<SysUser> user = remoteBaseService.getCurrentUser(token);
		SysUser userInfo = null;
		
		if (user != null) {
			userInfo = user.getData();
		} else {
			r.setMsg("系统异常！");
			return r;
		}
		try {
			WarnSchemeEntity entity = new WarnSchemeEntity();
			entity.setName(name);
			entity.setDescription(description);
			entity.setMainType(mainType);
			entity.setType(type);
			entity.setDeptId(String.valueOf(userInfo.getOfficeId()));
			outInterfaceService.addScheme(entity,null,file);
			r.setMsg("添加方案成功！");
		} catch (Exception e) {
			r.setCode(RestServiceResult.CODE_UNKNOW);
			r.setMsg("系统异常！");
		}
		return r;
	}
	
	/**
	   *      添加方案
	 * @param entity
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/downLoadMb.json")
	@IgnoreAuthority
	public RestServiceResult<Object> downLoadMb(
			@RequestParam(value = "mainType", defaultValue = "",required = true)String mainType,
        HttpServletResponse response) {
		RestServiceResult<Object> r = new RestServiceResult<>();
		
		if ("100000".equals(mainType)) {
        	com.py.credit.utils.FileUtils.downloadFile(this.getClass().getResource("/" + "导入预警企业.xlsx").getPath(), response);
        }
        if ("100001".equals(mainType)) {
        	com.py.credit.utils.FileUtils.downloadFile(this.getClass().getResource("/" + "导入预警人员.xlsx").getPath(), response);
        }
        if ("100002".equals(mainType)) {
        	com.py.credit.utils.FileUtils.downloadFile(this.getClass().getResource("/" + "导入预警非企业法人.xlsx").getPath(), response);
        }
		
		return r;
	}
	
	
	
	/**
	 * 修改方案
	 * @param entity
	 * @return
	 */
	@RequestMapping(value = "/updateScheme.json", method = RequestMethod.POST)
	public RestServiceResult<Object> updateScheme(
			@RequestParam(value = "recordId", defaultValue = "",required = true)String recordId,
			@RequestParam(value = "name", defaultValue = "",required = true)String name,
			@RequestParam(value = "type", defaultValue = "",required = true)String type,
			@RequestParam(value = "mainType", defaultValue = "",required = true)String mainType,
			@RequestParam(value = "description", defaultValue = "",required = true)String description,
			@RequestParam(value = "tybms", defaultValue = "",required = true)List<String> tybms) {
		RestServiceResult<Object> r = new RestServiceResult<>();
		try {
			WarnSchemeEntity entity = new WarnSchemeEntity();
			entity.setRecordId(recordId);
			entity.setName(name);
			entity.setDescription(description);
			entity.setMainType(mainType);
			entity.setType(type);
			outInterfaceService.updateScheme(entity,tybms,null);
			r.setMsg("修改方案成功！");
		} catch (Exception e) {
			r.setCode(RestServiceResult.CODE_UNKNOW);
			r.setMsg("系统异常！");
		}
		return r;
	}


	/**
	 * 修改方案
	 * @param entity
	 * @return
	 */
	@RequestMapping(value = "/updateSchemeByFile.json", method = RequestMethod.POST)
	public RestServiceResult<Object> updateSchemeByFile(
			@RequestParam(value = "recordId", defaultValue = "",required = true)String recordId,
			@RequestParam(value = "name", defaultValue = "",required = true)String name,
			@RequestParam(value = "type", defaultValue = "",required = true)String type,
			@RequestParam(value = "mainType", defaultValue = "",required = true)String mainType,
			@RequestParam(value = "description", defaultValue = "",required = true)String description,
			@RequestParam(value = "file", defaultValue = "",required = false)MultipartFile file) {
		RestServiceResult<Object> r = new RestServiceResult<>();
		try {
			WarnSchemeEntity entity = new WarnSchemeEntity();
			entity.setRecordId(recordId);
			entity.setName(name);
			entity.setDescription(description);
			entity.setMainType(mainType);
			entity.setType(type);
			outInterfaceService.updateScheme(entity,null,file);
			r.setMsg("修改方案成功！");
		} catch (Exception e) {
			r.setCode(RestServiceResult.CODE_UNKNOW);
			r.setMsg("系统异常！"+e.getMessage());
		}
		return r;
	}

	
	
	/**
	    *    更新接口状态
	 * @param status
	 * @param recordId
	 * @param mainType
	 * @return
	 */
	@RequestMapping(value = "/updateSchemeStatus.json", method = RequestMethod.POST)
	public RestServiceResult<Object> updateSchemeStatus(String status, String recordId, String mainType) {
		RestServiceResult<Object> r = new RestServiceResult<>();
		try {
			outInterfaceService.updateSchemeStatus(status, recordId);
			;
			r.setMsg("修改方案成功！");
		} catch (Exception e) {
			r.setCode(RestServiceResult.CODE_UNKNOW);
			r.setMsg("系统异常！");
		}
		return r;
	}

	/**
	    *      列表展示方案
	 * @param name
	 * @param page
	 * @return
	 */
	@RequestMapping(value = "/selectSchemeForPage.json", method = RequestMethod.GET)
	public RestServiceResult<Object> selectSchemeForPage(String name,String type,String deptName, Page page,HttpServletRequest request
			) {
		RestServiceResult<Object> r = new RestServiceResult<>();
		RestServiceResult<SysUser> user = remoteBaseService.getCurrentUser(HttpServletHelper.getUserToken(request));
		SysUser userInfo = null;

		if (RestServiceResult.CODE_SUCCESS == user.getCode()) {
			userInfo = user.getData();
			
		} else {
			
			throw new ServiceException("未获取到当前用户信息！");
		}
		Long userId = userInfo.getId();
		Long deptId = userInfo.getOfficeId();
		String rolename = userInfo.getRoleName();

		try {
			if(rolename.equals("系统管理员")) {
				r.setData(outInterfaceService.selectSchemeForPage(name, page,type,deptName,null,null));
			}else if(rolename.equals("部门管理员")) {
				r.setData(outInterfaceService.selectSchemeForPage(name, page,type,deptName,deptId,null));
			}else {
				r.setData(outInterfaceService.selectSchemeForPage(name, page,type,deptName,deptId,userId));
			}
			
			r.setCount(page.getTotalCount());
		} catch (Exception e) {
			r.setCode(RestServiceResult.CODE_UNKNOW);
			r.setMsg("系统异常！");
		}
		return r;
	}
	
	@IgnoreAuthority
	@RequestMapping(value = "/selectWarnDept.json")
	public RestServiceResult<List<Map<String,String>>> selectWarnDept() {
		RestServiceResult<List<Map<String,String>>> r = new RestServiceResult<List<Map<String,String>>>();
		try {
			r.setData(outInterfaceService.selectWarnDept());
		} catch (Exception e) {
			r.setCode(RestServiceResult.CODE_UNKNOW);
			r.setMsg("系统异常！");
		}
		return r;
	}
	@RequestMapping(value = "/deleteScheme.json")
	public RestServiceResult<Object> deleteScheme(String ids) {
		RestServiceResult<Object> r = new RestServiceResult<>();
		String[] record_ids = ids.split(",");
		try {
		for (String id : record_ids) {
			outInterfaceService.delete(Integer.valueOf(id));
		}				
		} catch (Exception e) {
			r.setCode(RestServiceResult.CODE_UNKNOW);
			r.setMsg("系统异常！");
		}
		return r;
	}
    /**
               *      添加方案、规则关联关系
     * @param ruleId
     * @param schemeId
     * @return
     */
	@RequestMapping(value = "/addRuleAssociation.json", method = RequestMethod.POST)
	public RestServiceResult<Object> addRuleAssociation(String ruleId, String schemeId) {
		RestServiceResult<Object> r = new RestServiceResult<>();
		try {
			outInterfaceService.addRuleAssociation(ruleId, schemeId);
			r.setMsg("添加规则关联成功！");
		} catch (Exception e) {
			r.setCode(RestServiceResult.CODE_UNKNOW);
			r.setMsg("系统异常！");
		}
		return r;
	}

	/**
	 *       解绑方案规则
	 * @param schemeId
	 * @return
	 */
	@RequestMapping(value = "/updateAssociationDel.json", method = RequestMethod.POST)
	public RestServiceResult<Object> updateAssociationDel(String schemeId) {
		RestServiceResult<Object> r = new RestServiceResult<>();
		try {
			outInterfaceService.updateAssociationDel(schemeId);
			;
			r.setMsg("解绑规则成功！");
		} catch (Exception e) {
			r.setCode(RestServiceResult.CODE_UNKNOW);
			r.setMsg("系统异常！");
		}
		return r;
	}

    /**
                *    删除规则
     * @param tybm
     * @return
     */
	@RequestMapping(value = "/deleteRule.json", method = RequestMethod.POST)
	public RestServiceResult<Object> deleteRule(String tybm) {
		RestServiceResult<Object> r = new RestServiceResult<>();
		try {
			outInterfaceService.deleteRuleByTybm(tybm);
			r.setMsg("添加规则关联成功！");
		} catch (Exception e) {
			r.setCode(RestServiceResult.CODE_UNKNOW);
			r.setMsg("系统异常！");
		}
		return r;
	}

    /**
               *  查询方案可绑规则
     * @param mainType
     * @return
     */
	@RequestMapping(value = "/selectRuleForBind.json", method = RequestMethod.GET)
	public RestServiceResult<Object> selectRuleForBind(String mainType) {
		RestServiceResult<Object> r = new RestServiceResult<>();
		try {
			r.setData(outInterfaceService.selectRuleForBind(mainType));
		} catch (Exception e) {
			r.setCode(RestServiceResult.CODE_UNKNOW);
			r.setMsg("系统异常！");
		}
		return r;
	}

	/**
	    *   更新规则
	 * @param entity
	 * @return
	 */
	@RequestMapping(value = "/updateRule.json", method = RequestMethod.POST)
	public RestServiceResult<Object> updateRule(@RequestBody WarnRuleEntity entity) {
		RestServiceResult<Object> r = new RestServiceResult<>();
		try {
			outInterfaceService.updateRuleById(entity);
			r.setMsg("修改规则成功！");
		} catch (Exception e) {
			r.setCode(RestServiceResult.CODE_UNKNOW);
			r.setMsg("系统异常！");
		}
		return r;
	}

	/**
	    *    删除规则
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/deleteRuleById.json", method = RequestMethod.POST)
	public RestServiceResult<Object> updateRule(@Param(value = "id") String id) {
		RestServiceResult<Object> r = new RestServiceResult<>();
		try {
			outInterfaceService.deleteRuleById(id);
			r.setMsg("删除规则成功！");
		} catch (Exception e) {
			r.setCode(RestServiceResult.CODE_UNKNOW);
			r.setMsg("系统异常！");
		}
		return r;
	}

	 /**
	  * 列表展示家政从业人员信息
	  * @param tybm
	  * @param name
	  * @param page
	  * @return
	  */
	@RequestMapping(value = "/getPeopleForPage.json", method = RequestMethod.GET)
	public RestServiceResult<Object> getPeopleForPage(String tybm, String name, Page page) {
		RestServiceResult<Object> r = new RestServiceResult<>();
		try {
			r.setData(outInterfaceService.getPeopleForPage(tybm, page, name));
		} catch (Exception e) {
			r.setCode(RestServiceResult.CODE_UNKNOW);
			r.setMsg("系统异常！");
		}
		return r;
	}

	@Deprecated
	@RequestMapping(value = "/getRelateComForPage.json", method = RequestMethod.GET)
	public RestServiceResult<Object> getRelateComForPage(String tybm, String name, Page page) {
		RestServiceResult<Object> r = new RestServiceResult<>();
		try {
			r.setData(outInterfaceService.getComForPage(tybm, page, name));
		} catch (Exception e) {
			r.setCode(RestServiceResult.CODE_UNKNOW);
			r.setMsg("系统异常！");
		}
		return r;
	}

	/**
	 * 定时将指标推送到credit库里
	 * @return
	 */
	@IgnoreAuthority
	@RequestMapping(value = "/addRealateIndex.do")
	public RestServiceResult<Object> addRealateIndex() {
		RestServiceResult<Object> r = new RestServiceResult<>();
		try {
			outInterfaceService.addRealateIndex();
		} catch (Exception e) {
			r.setCode(RestServiceResult.CODE_UNKNOW);
			r.setMsg("系统异常！");
		}
		return r;
	}
	
	/**
	 * 定时将跑分的结果更新到credit库里
	 * @return
	 */
	@IgnoreAuthority
	@RequestMapping(value = "/addModelTest.do", method = RequestMethod.GET)
	public RestServiceResult<Object> addModelTest() {
		RestServiceResult<Object> r = new RestServiceResult<>();
		try {
			outInterfaceService.addModelTest();
		} catch (Exception e) {
			r.setCode(RestServiceResult.CODE_UNKNOW);
			r.setMsg("系统异常！");
		}
		return r;
	}
    /**
     * 列表展示规则信息
     * @param type
     * @param name
     * @param page
     * @return
     */
	@RequestMapping(value = "/selectRuleForPage.json", method = RequestMethod.GET)
	public RestServiceResult<Object> selectRuleForPage(String type, String name, Page page,HttpServletRequest request) {
		RestServiceResult<Object> r = new RestServiceResult<>();

		RestServiceResult<SysUser> user = remoteBaseService.getCurrentUser(HttpServletHelper.getUserToken(request));
		SysUser userInfo = null;

		if (RestServiceResult.CODE_SUCCESS == user.getCode()) {
			userInfo = user.getData();			
		} else {			
			throw new ServiceException("未获取到当前用户信息！");
		}
		Long userId = userInfo.getId();
		Long deptId = userInfo.getOfficeId();
		String rolename = userInfo.getRoleName();
		List<WarnRuleEntity> map = new ArrayList<>();
		try {
			if(rolename.equals("系统管理员")) {
				 map = outInterfaceService.selectRuleForPage(name, type, page,null,null);
			}else if(rolename.equals("部门管理员")) {
				 map = outInterfaceService.selectRuleForPage(name, type, page,deptId,null);
			}else {
				 map = outInterfaceService.selectRuleForPage(name, type, page,deptId,userId);
			}
			
			r.setData(map);
			r.setCount(page.getTotalCount());
		} catch (Exception e) {
			r.setCode(RestServiceResult.CODE_UNKNOW);
			r.setMsg("系统异常！");
		}
		return r;
	}
	
	
	/**
	 * 定时更新企业预警信息
	 * @return
	 */
	@IgnoreAuthority
	@RequestMapping(value = "/addComInfo.do")
	public RestServiceResult<Object> addComInfo() {
		RestServiceResult<Object> r = new RestServiceResult<>();
		List<Map<String, String>> paramList = new ArrayList<>();
		try {
			List<Map<String,Object>> map = outInterfaceService.selectComInfo();
			String scemeId = "";
			for (Map<String,Object> qy : map) {
				String str = String.valueOf(qy.get("tybm"));
				String schemeId = String.valueOf(qy.get("schemeId"));
				if(!scemeId.equals(schemeId)) {
					scemeId = schemeId;
				}
				Map<String, Object> data = outInterfaceService.getRuleByComTask(null, schemeId, str);
				Integer count = Integer.valueOf(String.valueOf(data.get("count")));
				if (count > 0) {
					Map<String, String> paramMap = new HashMap<>();
					paramMap.put("tybm", str);
					paramMap.put("type", "100000");
					paramMap.put("scheme", String.valueOf(data.get("scheme")));
					paramList.add(paramMap);
				}
			}
			String tybm = String.valueOf(map);
			String tybmRepair = tybm.substring(1, tybm.length() - 1);
			List<Map<String, Object>> ruleData = outInterfaceService.getModelTestData(tybmRepair);
			if (ruleData != null) {
				for (Map<String, Object> item : ruleData) {
					Map<String, Object> data = outInterfaceService.getRuleByComTask(item, scemeId, "");
					Integer count = Integer.valueOf(String.valueOf(data.get("count")));
					if (count > 0) {
						Map<String, String> paramMap = new HashMap<>();
						paramMap.put("tybm", String.valueOf(item.get("tybm")));
						paramMap.put("type", "100000");
						paramMap.put("scheme", String.valueOf(data.get("scheme")));
						paramList.add(paramMap);
					}
				}
			}
			if (paramList.size() > 0) {
				outInterfaceService.addStatistics(paramList);
			}
		} catch (Exception e) {
			r.setCode(RestServiceResult.CODE_UNKNOW);
			r.setMsg("系统异常！");
		}
		return r;
	}

	/**
	 * 定时任务更新家政从业人员预警信息
	 * @return
	 */
	@IgnoreAuthority
	@RequestMapping(value = "/addPersonInfo.do")
	public RestServiceResult<Object> addPersonInfo() {
		RestServiceResult<Object> r = new RestServiceResult<>();
		List<Map<String, String>> paramList = new ArrayList<>();
		try {
			List<Map<String,Object>> map = outInterfaceService.selectPersonInfo();
			String scemeId = "";
			for (Map<String,Object> qy : map) {
				String str = String.valueOf(qy.get("tybm"));
				String schemeId = String.valueOf(qy.get("schemeId"));
				if(!scemeId.equals(schemeId)) {
					scemeId = schemeId;
				}
				Map<String, Object> data = outInterfaceService.getRuleByComTask(null, schemeId, str);
				Integer count = Integer.valueOf(String.valueOf(data.get("count")));
				if (count > 0) {
					Map<String, String> paramMap = new HashMap<>();
					paramMap.put("tybm", str);
					paramMap.put("type", "100002");
					paramMap.put("scheme", String.valueOf(data.get("scheme")));
					paramList.add(paramMap);
				}
			}
			String tybm = String.valueOf(map);
			String tybmRepair = tybm.substring(1, tybm.length() - 1);
			List<Map<String, Object>> ruleData = outInterfaceService.getModelTestData(tybmRepair);
			if (ruleData != null) {
				for (Map<String, Object> item : ruleData) {
					Map<String, Object> data = outInterfaceService.getRuleByComTask(item, scemeId, "");
					Integer count = Integer.valueOf(String.valueOf(data.get("count")));
					if (count > 0) {
						Map<String, String> paramMap = new HashMap<>();
						paramMap.put("tybm", String.valueOf(item.get("tybm")));
						paramMap.put("type", "100002");
						paramMap.put("scheme", String.valueOf(data.get("scheme")));
						paramList.add(paramMap);
					}
				}
			}
			if (paramList.size() > 0) {
				outInterfaceService.addStatistics(paramList);
			}
		} catch (Exception e) {
			r.setCode(RestServiceResult.CODE_UNKNOW);
			r.setMsg("系统异常！");
		}
		return r;
	}
	
	
	/**
	 * 定时更新企业预警信息
	 * @return
	 */
	@IgnoreAuthority
	@RequestMapping(value = "/addUnComInfo.do")
	public RestServiceResult<Object> addUnComInfo() {
		RestServiceResult<Object> r = new RestServiceResult<>();
		List<Map<String, String>> paramList = new ArrayList<>();
		try {
			List<Map<String,Object>> map = outInterfaceService.selectUnComInfo();
			String scemeId = "";
			for (Map<String,Object> qy : map) {
				String str = String.valueOf(qy.get("tybm"));
				String schemeId = String.valueOf(qy.get("schemeId"));
				if(!scemeId.equals(schemeId)) {
					scemeId = schemeId;
				}
				Map<String, Object> data = outInterfaceService.getRuleByComTask(null, schemeId, str);
				Integer count = Integer.valueOf(String.valueOf(data.get("count")));
				if (count > 0) {
					Map<String, String> paramMap = new HashMap<>();
					paramMap.put("tybm", str);
					paramMap.put("type", "100000");
					paramMap.put("scheme", String.valueOf(data.get("scheme")));
					paramList.add(paramMap);
				}
			}
			String tybm = String.valueOf(map);
			String tybmRepair = tybm.substring(1, tybm.length() - 1);
			List<Map<String, Object>> ruleData = outInterfaceService.getModelTestData(tybmRepair);
			if (ruleData != null) {
				for (Map<String, Object> item : ruleData) {
					Map<String, Object> data = outInterfaceService.getRuleByComTask(item, scemeId, "");
					Integer count = Integer.valueOf(String.valueOf(data.get("count")));
					if (count > 0) {
						Map<String, String> paramMap = new HashMap<>();
						paramMap.put("tybm", String.valueOf(item.get("tybm")));
						paramMap.put("type", "100000");
						paramMap.put("scheme", String.valueOf(data.get("scheme")));
						paramList.add(paramMap);
					}
				}
			}
			if (paramList.size() > 0) {
				outInterfaceService.addStatistics(paramList);
			}
		} catch (Exception e) {
			r.setCode(RestServiceResult.CODE_UNKNOW);
			r.setMsg("系统异常！");
		}
		return r;
	}

	
	
	@IgnoreAuthority
	@RequestMapping(value = "/getWarnInfoData.json", method = RequestMethod.GET)
	public RestServiceResult<Object> getWarnInfoData(String type, String name,String tybm, Page page,HttpServletRequest request) {
		RestServiceResult<Object> r = new RestServiceResult<>();
		RestServiceResult<SysUser> user = remoteBaseService.getCurrentUser(HttpServletHelper.getUserToken(request));
		String token = HttpServletHelper.getUserToken(request);
	    SysUser userInfo = user.getData();
	    
	    long deptId = userInfo.getOfficeId();
	    String deptid = String.valueOf(userInfo.getOfficeId());
	  //获取当前用户的角色
	    String rolename=userInfo.getRoleName();
		try {
			if(rolename.equals("系统管理员")) {
				List<Map<String, String>> map = outInterfaceService.getWarnInfoData(type, name, page,tybm,null);
				r.setData(map);
				r.setCount(page.getTotalCount());
			}else {
				List<Map<String, String>> map = outInterfaceService.getWarnInfoData(type, name, page,tybm,deptid);
				r.setData(map);
				r.setCount(page.getTotalCount());
			}
			
			
		} catch (Exception e) {
			r.setCode(RestServiceResult.CODE_UNKNOW);
			r.setMsg("系统异常！");
		}
		return r;
	}
	
	@RequestMapping(value = "/reportDownLod.json")
	public void downlodReport(@RequestParam(value = "tybm", defaultValue = "", required = true) String tybm,
			HttpServletResponse response, HttpServletRequest request) {
		List<Map<String, Object>> data = outInterfaceService.getCreditReportData(tybm);
		if (StringUtils.isBlank(String.valueOf(data.get(0).get("LOCATION")))
				|| !(new File(absolutepath + String.valueOf(data.get(0).get("LOCATION"))).exists())) {

			throw new ServiceException("报告已不存在,请重新生成");

		}

		RestServiceResult<SysUser> user = remoteBaseService.getCurrentUser(HttpServletHelper.getUserToken(request));

		if (RestServiceResult.CODE_SUCCESS == user.getCode()) {

			SysUser userInfo = user.getData();

			if (null != userInfo) {

				String deptName = userInfo.getOfficeName();
				String realName = userInfo.getRealName();
				String userName = userInfo.getUsername();
				CreditReportLog log = new CreditReportLog(null, Integer.valueOf(String.valueOf(data.get(0).get("ID"))),
						new Date(), userName, realName, deptName);
				creditReportService.addReportLog(log);

			} else {

				ExceptionLogger.error("CreditCheckHistoryController#addShowColunms", "未获取到当前用户信息！");
			}

		} else {

			ExceptionLogger.error("CreditCheckHistoryController#addShowColunms", "未获取到当前用户信息！");
		}

		FileUtil.downloadFile(absolutepath + data.get(0).get("LOCATION"), response);

	}
	
	@IgnoreAuthority
	@RequestMapping(value = "/getWarnCompany.json", method = RequestMethod.POST)
	public RestServiceResult<Object> getWarnCompany(String mainType, String recordId,String type, Page page,HttpServletRequest request) {
		RestServiceResult<Object> r = new RestServiceResult<>();
		RestServiceResult<SysUser> user = remoteBaseService.getCurrentUser(HttpServletHelper.getUserToken(request));
	    SysUser userInfo = user.getData();
	    String rolename=userInfo.getRoleName();
	    long deptId = userInfo.getOfficeId();
	    String deptid = String.valueOf(userInfo.getOfficeId());
	  
		try {			
				List<Map<String, String>> map = outInterfaceService.getWarnCompanyForPage(type, recordId, mainType,page);
				r.setData(map);
				r.setCount(page.getTotalCount());
		
		} catch (Exception e) {
			r.setCode(RestServiceResult.CODE_UNKNOW);
			r.setMsg("系统异常！");
		}
		return r;
	}
	
	@IgnoreAuthority
	@RequestMapping(value = "/getIsWarnCompany.json", method = RequestMethod.POST)
	public RestServiceResult<Object> getIsWarnCompany(String mainType, String recordId,String type, Page page,HttpServletRequest request) {
		RestServiceResult<Object> r = new RestServiceResult<>();
		RestServiceResult<SysUser> user = remoteBaseService.getCurrentUser(HttpServletHelper.getUserToken(request));
	    SysUser userInfo = user.getData();
	    String rolename=userInfo.getRoleName();
	    long deptId = userInfo.getOfficeId();
	    String deptid = String.valueOf(userInfo.getOfficeId());
	  
		try {			
				List<Map<String, String>> map = outInterfaceService.getIsWarnCompanyForPage(type, recordId, mainType,page);
				r.setData(map);
				r.setCount(page.getTotalCount());
		
		} catch (Exception e) {
			r.setCode(RestServiceResult.CODE_UNKNOW);
			r.setMsg("系统异常！");
		}
		return r;
	}
}
